using System;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
namespace NoteManger
{
	public class AccessDBObject : DataBaseObject
	{
		protected OleDbConnection aAccessConnection = new OleDbConnection();
		public override bool Connect(string mdbFilePath)
		{
			bool result;
			try
			{
				string systemDirectory = Environment.SystemDirectory;
				string text = "Provider=Microsoft.Jet.OleDb.4.0;";
				text = text + "Data Source=" + mdbFilePath;
				this.aAccessConnection.Close();
				this.aAccessConnection.ConnectionString = text;
				this.aAccessConnection.Open();
				this._Connected = true;
				result = true;
			}
			catch (Exception ex)
			{
				this._Message = ex.Message;
				this._Connected = false;
				result = false;
			}
			return result;
		}
		public override bool Connect(string aServerName, string aUserName, string aPassword, string aDataBaseName)
		{
			this._Message = "派生类不支持该重载版本的函数。";
			this._Connected = false;
			return false;
		}
		public override bool Connect(string aDataSource, string aUserName, string aPassword)
		{
			this._Message = "派生类不支持该重载版本的函数。";
			this._Connected = false;
			return false;
		}
		public override void DisConnect()
		{
			try
			{
				this.aAccessConnection.Close();
			}
			catch (Exception ex)
			{
				this._Message = ex.Message;
			}
		}
		public override string[,] Open(string aStrSql)
		{
			this._Message = "";
			string[,] array = new string[0, 0];
			string[,] result;
			if (!this._Connected)
			{
				this._Message = "数据库尚未连接！";
				result = array;
			}
			else
			{
				OleDbCommand oleDbCommand = new OleDbCommand();
				try
				{
					oleDbCommand.Connection = this.aAccessConnection;
					oleDbCommand.CommandText = aStrSql;
					OleDbDataReader oleDbDataReader = oleDbCommand.ExecuteReader();
					if (oleDbDataReader.HasRows)
					{
						int num = 0;
						while (oleDbDataReader.Read())
						{
							for (int i = 0; i <= oleDbDataReader.FieldCount - 1; i++)
							{
								array = (string[,])DataBaseObject.ChangeArrayLength(array, num + 1, oleDbDataReader.FieldCount);
								array[num, i] = oleDbDataReader.GetValue(i).ToString();
							}
							num++;
						}
						oleDbDataReader.Close();
						oleDbDataReader.Dispose();
						oleDbCommand.Dispose();
						result = array;
					}
					else
					{
						oleDbDataReader.Close();
						oleDbDataReader.Dispose();
						oleDbCommand.Dispose();
						result = array;
					}
				}
				catch (Exception ex)
				{
					this._Message = ex.Message.ToString();
					oleDbCommand.Dispose();
					result = array;
				}
				finally
				{
				}
			}
			return result;
		}
		public override void Open(string aStrSql, DataGridView objDataGridView)
		{
			this._Message = "";
			OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter();
			OleDbCommand oleDbCommand = new OleDbCommand();
			DataSet dataSet = new DataSet();
			try
			{
				oleDbCommand.Connection = this.aAccessConnection;
				oleDbCommand.CommandText = aStrSql;
				oleDbDataAdapter.SelectCommand = oleDbCommand;
				dataSet.Clear();
				oleDbDataAdapter.Fill(dataSet, "table");
				objDataGridView.DataSource = dataSet.Tables[0].DefaultView;
			}
			catch (Exception ex)
			{
				this._Message = ex.Message.ToString();
			}
			finally
			{
				oleDbDataAdapter.Dispose();
				oleDbCommand.Dispose();
				dataSet.Dispose();
			}
		}
		public override int ExecSQL(string aStrSql)
		{
			this._Message = "";
			int result;
			if (!this._Connected)
			{
				this._Message = "数据库尚未连接！";
				result = -1;
			}
			else
			{
				OleDbCommand oleDbCommand = new OleDbCommand();
				try
				{
					oleDbCommand.Connection = this.aAccessConnection;
					oleDbCommand.CommandText = aStrSql;
					int num = oleDbCommand.ExecuteNonQuery();
					result = num;
				}
				catch (Exception ex)
				{
					this._Message = ex.Message.ToString();
					result = -1;
				}
				finally
				{
					oleDbCommand.Dispose();
				}
			}
			return result;
		}
	}
}
